﻿using System;
using System.Windows;
using System.IO;
//using System.Windows.Forms;
using Shoozla.ViewModel;
using MahApps.Metro.Controls;


namespace Shoozla
{
    public partial class FolderExplorerView : MetroWindow
    {
        private FolderExplorerViewModel viewModel;

        public FolderExplorerView() : base()
        {
           InitializeComponent();
           viewModel = new FolderExplorerViewModel(foldersTree, this);
           DataContext = viewModel;

           viewModel.RequestClose += (s, e) => this.Close();
  
           //foldersTree.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(viewModel.foldersTree_SelectedItemChanged);
        }

        public void folder_Checked(object sender, RoutedEventArgs e)
        {
            viewModel.folder_Checked(sender, e);
        }

        public void folder_UnChecked(object sender, RoutedEventArgs e)
        {
            viewModel.folder_UnChecked(sender, e);
        }


        #region Drag&Drop
        

        private void MetroWindow_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effects = DragDropEffects.Copy;
            else
                e.Effects = DragDropEffects.None;

        }

        private void MetroWindow_DragLeave(object sender, DragEventArgs e)
        {

        }

        private void MetroWindow_DragOver(object sender, DragEventArgs e)
        {

        }

        private void MetroWindow_Drop(object sender, DragEventArgs e)
        {
            bool error = false;
            try
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    string[] dir = (string[])e.Data.GetData(DataFormats.FileDrop);
                    if (Directory.Exists(dir[0]))
                    {                        
                        viewModel.AddFolderToView(new DirectoryInfo(dir[0]));
                    }
                    else
                        error = true;
                }
            }
            catch (Exception ex)
            {
                error = true;

            }
            finally
            {
                if (error)
                    MetroMessageBox.MetroShow("", "You can drop only one Directory", true, this, MessageIcon.Warning, false);

            }

        }
        #endregion
    }
}
